<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>设置</title>
<meta name="viewport" content="width=100%, initial-scale=1.0, user-scalable=no" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="/lib/weix/style/weui.min.css">
<link rel="stylesheet" href="/css/fonts.css">
<link rel="stylesheet" href="/css/comm.css">
<link rel="stylesheet" href="/css/gl_shez.css">
</head>

<body>
<form>
<div class="weui-cells__title" style="font-size:22px; color:#000">报备</div>
<div class="weui-cells weui-cells_form">
    <div class="weui-cell">
        <div class="weui-cell__hd"><label class="weui-label">有效期(天)</label></div>
        <div class="weui-cell__bd">
            <input class="weui-input" type="tel" placeholder="" value="" id="J_data_sj">
        </div>
    </div>
</div>
<div class="weui-cells__tips">预计到店时间，超过时限则不能优惠开单。</div>

<div class="weui-cells">
    <div class="weui-cell weui-cell_switch">
        <div class="weui-cell__bd">是否发放优惠券</div>
        <div class="weui-cell__ft">
            <label for="J_yhq_sf" class="weui-switch-cp">
                <input id="J_yhq_sf" class="weui-switch-cp__input" type="checkbox">
                <div class="weui-switch-cp__box"></div>
            </label>
        </div>
    </div>
    <a class="weui-cell weui-cell_access" href="javascript:;" id="J_quan" data-leix="" data-man="" data-youh="" style="display:none;">
        <div class="weui-cell__bd">
            <p>优惠券</p>
        </div>
        <div class="weui-cell__ft" id="J_yhq_html"></div>
    </a>
</div>
<div class="weui-cells__tips">合伙人推荐客户时发放给业主的消费优惠券。</div>


<div class="weui-cells__title" style=" margin-top:30px; font-size:22px; color:#000">合伙人</div>
<div class="weui-cells weui-cells_form">
    <div class="weui-cell weui-cell_switch">
        <div class="weui-cell__bd">发展合伙人奖励</div>
        <div class="weui-cell__ft">
            <label for="J_hehr" class="weui-switch-cp">
                <input id="J_hehr" class="weui-switch-cp__input" type="checkbox">
                <div class="weui-switch-cp__box"></div>
            </label>
        </div>
    </div>
    <div class="weui-cell" id="J_hehr_jfbox" style="display:none;">
        <div class="weui-cell__hd"><label class="weui-label">积分奖励</label></div>
        <div class="weui-cell__bd">
            <input class="weui-input no" type="number" placeholder="" value="100" id="J_hehr_jf">
        </div>
    </div>
</div>
<div class="weui-cells__tips">A成功发展一名合伙人B，A获得相应奖励。</div>

<div class="weui-cells weui-cells_form">
    <div class="weui-cell weui-cell_switch">
        <div class="weui-cell__bd">合伙人上级分成</div>
        <div class="weui-cell__ft">
            <label for="J_shanj" class="weui-switch-cp">
                <input id="J_shanj" class="weui-switch-cp__input" type="checkbox">
                <div class="weui-switch-cp__box"></div>
            </label>
        </div>
    </div>
    <div class="weui-cell" id="J_shanj_blbox" style="display:none;">
        <div class="weui-cell__hd"><label class="weui-label">分成比例%</label></div>
        <div class="weui-cell__bd">
            <input class="weui-input no" type="number" placeholder="" value="5" id="J_shanj_bl">
        </div>
    </div>
</div>
<div class="weui-cells__tips on">A发展B为合伙人，B推荐客户成交一单，B获得积分的同时A也获得B积分相应比例的积分。</div>

<div class="weui-btn-area">
    <a class="weui-btn weui-btn_primary" href="javascript:" id="J_qued">完成</a>
</div>
</form>

<!--优惠券弹出-->
<div class="th-tcc__boxs" id="J_tcc_quan">
	<div class="th-tcc__bj J_tcc_quan_clo"></div>
    <div class="th-tcc__box th-tcc__box-bot">
    	<div class="th-tcc__hd-bot th-xx_bot">优惠券设置<a href="javascript:;" class="clo J_tcc_quan_clo"><i class="icon-close"></i></a></div>
        <div class="th-tcc__bd-bot ft-bot">
        	
            <div class="weui-cells weui-cells_checkbox checkbox_boxs">
                <div class="weui-cell" style="padding-right:0;">
                    <div class="weui-cell__hd" style="padding-right:0;"><label class="weui-label">积分方式</label></div>
                </div>
                <label class="weui-cell weui-check__label" style="padding-left:0; padding-right:5px;">
                    <div class="weui-cell__hd">
                        <input type="radio" class="weui-check J_quan_leix" name="quan_leix" checked="checked" value="0">
                        <i class="weui-icon-checked"></i>
                    </div>
                    <div class="weui-cell__bd">
                        <p>满减券</p>
                    </div>
                </label>
                <label class="weui-cell weui-check__label" style="padding-left:0; padding-right:5px;">
                    <div class="weui-cell__hd">
                        <input type="radio" class="weui-check J_quan_leix" name="quan_leix"  value="1">
                        <i class="weui-icon-checked"></i>
                    </div>
                    <div class="weui-cell__bd">
                        <p>折扣券</p>
                    </div>
                </label>
            </div>
            
            <div class="weui-cells weui-cells_form" style="margin-top:0">
                <div class="weui-cell">
                    <div class="weui-cell__hd"><label class="weui-label">满(元)</label></div>
                    <div class="weui-cell__bd">
                        <input class="weui-input" type="number" placeholder="消费满多少" id="J_yhq_man">
                    </div>
                </div>
                <div class="weui-cell" id="J_yhq_jian_box">
                    <div class="weui-cell__hd"><label class="weui-label">减(元)</label></div>
                    <div class="weui-cell__bd">
                        <input class="weui-input" type="number" placeholder="0＜优惠范围≤订单金额" id="J_yhq_jian">
                    </div>
                </div>
                <div class="weui-cell" id="J_yhq_zhe_box"c style="display:none">
                    <div class="weui-cell__hd"><label class="weui-label">折扣</label></div>
                    <div class="weui-cell__bd">
                        <input class="weui-input" type="number" placeholder="0＜折扣范围＜10" id="J_yhq_zhe">
                    </div>
                </div>
            </div>

            
        </div>
        
        <div class="th-tcc__ft-bot"><a class="ans" href="javascript:;" id="J_yhq_ok">完成</a></div>
        
    </div>
</div>


<script src="/js/zepto.min.js"></script>
<script src="https://res.wx.qq.com/open/libs/weuijs/1.0.0/weui.min.js"></script>
<script src="/js/comm.js"></script>
<script type="text/javascript">
//查看设置
function config_look(){
	var uid= "${uid}";
	var data = {uid:uid};
	var url = "/config/config";
	$.ajax({
		type: "POST",
		async: false,
		dataType: 'json',
		contentType: "application/json; charset=utf-8",
		data: JSON.stringify(data),
		url: url,
		success: function(res){
			if(res.code == 200){
				var datas = res.data
				
				$('#J_data_sj').val(datas.indate)							//报备有效期
				
				if(datas.iscoupon == 1){									//是否开启优惠券 0关闭 1开启
					$('#J_yhq_sf').attr('checked','checked')				//优惠券-是否开启
					$('#J_quan').show()
					$('#J_quan').attr('data-leix',datas.discountType)		//优惠券-类型  0满减 1打折
					$('#J_quan').attr('data-man',datas.full)				//优惠券-满足额
					$('#J_quan').attr('data-youh',datas.discount)			//优惠券-优惠额
					
					//显示优惠券
					if(datas.discountType == 0){
						$('#J_yhq_html').html('优惠'+datas.discount+'(满'+datas.full+'使用)')
					}else if(datas.discountType == 1){
						$('#J_yhq_html').html(datas.discount+'折(满'+datas.full+'使用)')
					}else{
						weui.topTips('优惠券类型有误');
						return false
					}
					
				}
				
				if(datas.isaward == 1){										//发展合伙人奖励 0无奖励 1奖励
					$('#J_hehr').attr('checked','checked')					//发展合伙人积分是否开启
					$('#J_hehr_jfbox').show()
					$('#J_hehr_jf').val(datas.pints)						//发展合伙人积分
				}
				
				if(datas.isdivide == 1){									//合伙人上级分成 0无分成 1有分成
					$('#J_shanj').attr('checked','checked')					//合伙人上级分成是否开启
					$('#J_shanj_blbox').show()
					$('#J_shanj_bl').val(datas.percent)						//合伙人上级比例
				}

			}
		},
		error: function(){

		},
		complete: function(){
		}
	});
}

//赋值优惠券
function yhq_fz(){
	var leix_data = $('#J_quan').attr('data-leix');
	var man_data = Number($('#J_quan').attr('data-man'));
	var youh_data = Number($('#J_quan').attr('data-youh'));
	
	if(leix_data == 0){  //0满减 1折扣
		$("input[name='quan_leix'][value='0']").attr("checked",true);
		$('#J_yhq_jian').val(youh_data)
		$('#J_yhq_jian_box').show()
		$('#J_yhq_zhe_box').hide()
	}else if(leix_data == 1){
		$("input[name='quan_leix'][value='1']").attr("checked",true);
		$('#J_yhq_zhe').val(youh_data)
		$('#J_yhq_jian_box').hide()
		$('#J_yhq_zhe_box').show()
	}
	
	$('#J_yhq_man').val(man_data)
}

$(function(){	
	//sessionStorage.setItem("need-refresh", true);  //返回上一页刷新
	//初始化
	config_look()  //查看
	//-----------------------------------------------------------------------
	//完成提交
	$(document).on('click','#J_qued',function(){

		var data_sj =  Number($('#J_data_sj').val().trim())					//报备有效期
		var yhq_ope = $('#J_yhq_sf').is(':checked')						//优惠券-是否开启
		var yhq_ope_data = ''
		var leix_data = $('#J_quan').attr('data-leix')						//优惠券-类型
		var man_data = Number($('#J_quan').attr('data-man').trim())			//优惠券-满足额
		var youh_data = Number($('#J_quan').attr('data-youh').trim())		//优惠券-优惠额
		var hehr_ope = $('#J_hehr').is(':checked')							//发展合伙人积分是否开启
		var hehr_ope_data = ''
		var hehr_jf = Number($('#J_hehr_jf').val().trim())					//发展合伙人积分
		var shanj_ope = $('#J_shanj').is(':checked')						//合伙人上级分成是否开启
		var shanj_ope_data = ''
		var shanj_bl = Number($('#J_shanj_bl').val().trim()) 				//合伙人上级比例
		
		//打开优惠券
		if(yhq_ope == true){
			yhq_ope_data = 1
		}else{
			yhq_ope_data = 0
		}
		
		//发展合伙人奖励
		if(hehr_ope == true){
			hehr_ope_data = 1
		}else{
			hehr_ope_data = 0
		}
		
		if(hehr_ope_data == 1){
			var ex = /^\d+$/;
			if (!ex.test(hehr_jf)) {
				weui.topTips('积分奖励填写有误');
				return false
			}

		}
		
		//合伙人上级分成		
		if(shanj_ope == true){
			shanj_ope_data = 1
		}else{
			shanj_ope_data = 0
		}
		
		if(shanj_ope_data == 1){

			if (shanj_bl<=0) {
				weui.topTips('分成比例填写有误');
				return false
			}

		}
		
		weui.dialog({
			title: '保存设置',
			content: '是否确定保存相关设置？',
			className: 'custom-classname',
			buttons: [{
				label: '取消',
				type: 'default',
				onClick: function () {}
			}, {
				label: '确定',
				type: 'primary',
				onClick: function () {
				
					var loading = weui.loading('数据提交中', {
						className: 'custom-classname'
					});
					
					
					var uid= "${uid}";
					var data = {uid:uid,indate:data_sj,iscoupon:yhq_ope_data,discountType:leix_data,full:man_data,discount:youh_data,isaward:hehr_ope_data,pints:hehr_jf,isdivide:shanj_ope_data,percent:shanj_bl};
					var url = "/config/edit_config";
					$.ajax({
						type: "POST",
						async: false,
						dataType: 'json',
						contentType: "application/json; charset=utf-8",
						data: JSON.stringify(data),
						url: url,
						success: function(res){
							if(res.code == 200){
								
								weui.toast('保存成功', {
									duration: 1500,
									className: 'custom-classname',
									callback: function(){ 
										
									}
								});
							}else{
								weui.topTips(res.msg);
							}
						},
						error: function(){
				
						},
						complete: function(){
							loading.hide();
						}
					});
					 
				}
			}]
		});


	})
	//-----------------------------------------------------------------------
	//关闭优惠券弹出
	$(document).on('click','.J_tcc_quan_clo',function(){
		$('#J_tcc_quan').removeClass('on')
		$('body').removeClass('fix_box')
		
		if($('#J_yhq_html').html() == ''){
			$('#J_yhq_sf').prop("checked",false)
			$('#J_quan').hide()
		}
	})
	//打开优惠券弹出
	$(document).on('click','#J_quan',function(){
		yhq_fz() //优惠券赋值
		$('body').addClass('fix_box')
		$('#J_tcc_quan').addClass('on')
	})
	//-----------------------------------------------------------------------
	//优惠券开关
	$('#J_yhq_sf').bind('change',function(){ 
		console.log($(this).is(':checked'))
		
		if($(this).is(':checked')){
			$('#J_quan').show()
			yhq_fz()
			//打开优惠券设置
			$('body').addClass('fix_box')
			$('#J_tcc_quan').addClass('on')
		}else{
			$('#J_quan').hide()
		}
	}); 
	//合伙人奖励开关
	$('#J_hehr').bind("change",function(){ 
		if($(this).is(':checked')){
			$('#J_hehr_jfbox').show()
		}else{
			$('#J_hehr_jfbox').hide()
		}
	}); 
	//合伙人上级奖励开关
	$('#J_shanj').bind("change",function(){ 
		if($(this).is(':checked')){
			$('#J_shanj_blbox').show()
		}else{
			$('#J_shanj_blbox').hide()
		}
	}); 
	//-----------------------------------------------------------------------
	//优惠券弹出类型
	$(document).on('click','.J_quan_leix',function(){
		var data = $('input[name="quan_leix"]:checked').val()
		
		if(data == 0){
			$('#J_yhq_jian_box').show()
			$('#J_yhq_zhe_box').hide()
		}else if(data == 1){
			$('#J_yhq_jian_box').hide()
			$('#J_yhq_zhe_box').show()
		}
	})
	
	//优惠券弹出填写操作
	$(document).on('click','#J_yhq_ok',function(){
		var leix_data = $('input[name="quan_leix"]:checked').val(); 
		var man_data = Number($('#J_yhq_man').val().trim());
		var youh_data = Number($('#J_yhq_jian').val().trim());
		var zhe_data = Number($('#J_yhq_zhe').val().trim());
		var youh_zhe_data = '';
		
		if(man_data <=0){
			weui.topTips('请填写正确的满足金额');
			return false
		}
		
		if(leix_data == 0 && youh_data <=0){
			weui.topTips('请填写正确的减去金额');
			return false
		}
		
		if(leix_data == 1 && (zhe_data <= 0 || zhe_data >= 10)){
			weui.topTips('请填写正确的折扣');
			return false
		}
		
		if(youh_data > man_data){
			weui.topTips('减去金额不能大于满足金额');
			return false
		}
		
		if(leix_data == 0){
			$('#J_yhq_html').html('优惠'+youh_data+'(满'+man_data+'使用)')
			youh_zhe_data = youh_data
		}else if(leix_data == 1){
			$('#J_yhq_html').html(zhe_data+'折(满'+man_data+'使用)')
			youh_zhe_data = zhe_data
		}else{
			weui.topTips('优惠券类型有误');
			return false
		}
		
		$('#J_quan').attr('data-leix',leix_data).attr('data-man',man_data).attr('data-youh',youh_zhe_data)
		$('#J_tcc_quan').removeClass('on')
		$('body').removeClass('fix_box')
		
	})
	
});
</script>
</body>
</html>